package com.dxy.app.widget.draw;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.dxy.app.widget.ThreadManager;

/**
  *Created by duxingyu on 2017/8/1.15:55
  *Email: 
  *Description:
  **/
public class PathView extends View {

  private Paint paint;
  private Path path;

  public PathView(Context context) {
    super(context);
    init();
  }

  public PathView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public PathView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }


  private void init(){
    paint =new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);

    path =new Path();

    point =new PointF();
    point.x =250;
    point.y =100;
  }

  private PointF point;
  @Override public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()){
      case MotionEvent.ACTION_DOWN:
      case MotionEvent.ACTION_MOVE:
        point.x =event.getX();
        point.y =event.getY();
        invalidate();
        break;
      case MotionEvent.ACTION_UP:
        //point.x =250;
        //point.y =100;
        resetState();
        break;
    }
    return true;
  }

  private int width,height;
  @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    Log.i(TAG, "onSizeChanged: "+w+","+h+","+oldw+","+oldh);
    this.width =w;
    this.height =h;
  }

  public static final String TAG="TAG_PathView";


  @Override protected void onDraw(Canvas canvas) {
    paint.setColor(Color.RED);
    //drawSecondOrderBezier(canvas);
    //drawThirdOderBezier(canvas);

    drawPathAndGraph(canvas);
    //drawLines(canvas);

  }


  /**
   * 绘制线条
   * */
  private void drawLines(Canvas canvas){
    path.moveTo(100,100);//该方法是将起始点坐标移动到指定位置

    path.lineTo(200,300);//参数是坐标，将当前指定坐标与上一个点相连

    path.lineTo(200,500);

    path.rLineTo(200,200);//参数是增量，而非坐标，即上一个坐标的基础上加上增量

    path.close();//将绘制到最后一点与其实坐标点连接起来

    canvas.drawPath(path,paint);
  }
  /**
   * 绘制混合图形
   * */
  private void drawPathAndGraph(Canvas canvas){

    //path.moveTo(100,100);//

    //path.lineTo(400,100);

    //RectF rect =new RectF(200,200,500,500);
    //path.arcTo(rect,0,180,false);


    path.addCircle(350,350,150, Path.Direction.CW);
    //RectF rectf =new RectF(200,200,500,500);
    //path.addRect(rectf, Path.Direction.CCW);

    //path.moveTo(700,300);
    //path.lineTo(600,600);

    path.lineTo(500,700);

    path.close();

    canvas.drawPath(path,paint);
  }
  /**
   * 绘制二阶贝塞尔曲线
   * */
  private void drawSecondOrderBezier(Canvas canvas){
    path.reset();//清除之前的状态，否则的话会导致之前的状态也被绘制
    path.moveTo(100,400);
    path.quadTo(point.x,point.y,600,400);
    canvas.drawPath(path,paint);
  }
  /**
   * 绘制三阶贝塞尔曲线
   * */
  private void drawThirdOderBezier(Canvas canvas){
    path.moveTo(100,400);
    path.cubicTo(200,400,point.x,point.y,500,400);
    canvas.drawPath(path,paint);
  }

  private void resetState(){

    ThreadManager.getShortPool().execute(()->{
      while (point.y!=400){
        if(point.y>400){
          point.y=point.y-10;
          if(point.y<=400){
            point.y=400;
          }
        }else if(point.y<400){
          point.y=point.y+10;
          if(point.y>=400){
            point.y=400;
          }
        }
        postInvalidate();
        try {
          Thread.sleep(10);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    });
  }
}
